﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Business;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frmState : Office2007Form
    {
        #region Fields
        StateCtrl m_StateCtrl = new StateCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmState()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmState_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_StateCtrl.Show(dgvState, bindingNavigatorState, txtStateId, txtStateName);
            SaveItem.Enabled = false;
        }
        #endregion

        #region Button Click Event
        private void btnDongY_Click(object sender, EventArgs e)
        {
            StateInfo info = new StateInfo(Convert.ToInt32(txtStateId.Text), txtStateName.Text);
            if (isNew == true)
            {
                m_StateCtrl.Add(info);
            }
            else
            {
                m_StateCtrl.Update(info);
            }
            m_StateCtrl.Show(dgvState, bindingNavigatorState, txtStateId, txtStateName);
            SaveItem.Enabled = false;
            isNew = false;
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_StateCtrl.Show(dgvState, bindingNavigatorState, txtStateId, txtStateName);
            SaveItem.Enabled = false;
        }
        #endregion

        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvState.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_StateCtrl.AddNewRow();
            m_Row["StateId"] = GetID.States(dgvState);
            m_Row["StateName"] = "";
            m_StateCtrl.AddNewState(m_Row);
            bindingNavigatorState.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_StateCtrl.Show(dgvState, bindingNavigatorState, txtStateId, txtStateName);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvState.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvState.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvState.SelectedRows)
                    {
                        bindingNavigatorState.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvState.Rows)
            {
                if (row.Cells["colStateName"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_StateCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Searching
        private void rbStateId_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Mã trạng thái";
        }

        private void rbStateName_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Tên trạng thái";
        }

        private void btnTKDongY_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (rbStateId.Checked == true)
                {
                    dt = m_StateCtrl.GetById(Convert.ToInt32(txtKeyWord.Text));
                }
                else if (rbStateName.Checked == true)
                {
                    dt = m_StateCtrl.GetByName(txtKeyWord.Text);
                }
                else
                {
                    dt = m_StateCtrl.GetByName(txtKeyWord.Text);
                }
                m_StateCtrl.ShowWithSources(dgvState, bindingNavigatorState, txtStateId, txtStateName, dt);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSearchAdvanced_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region DataGridView Event
        private void dgvState_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }
        #endregion 

        #region Context Menu Event Click
        private void AddNewItemContextState_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContextState_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContextState_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContextState_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContextState_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
